﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Serilog.Events;
using Serilog;

namespace S.Transplantation.Api.Extensions.Filter
{
    /// <summary>
    /// 全局异常
    /// </summary>
    public class AllExceptionFilter : IAsyncExceptionFilter
    {
        private readonly ILogger<AllExceptionFilter> logger;

        public AllExceptionFilter(ILogger<AllExceptionFilter> logger)
        {
            this.logger = logger;
        }

        public Task OnExceptionAsync(ExceptionContext context)
        {
            if (!context.ExceptionHandled)
            {
                context.Result = new ContentResult()
                {
                    Content = context.Exception.Message,
                };
            }
            logger.LogError(context.Exception.Message);
            logger.LogError("异常");
            logger.LogError(context.Exception.StackTrace);

            context.ExceptionHandled = true;
            return Task.CompletedTask;
        }
        

    }
    
}